import {
    getEmployeeList,

} from "@/apis/common.js";

import {
    orderAudit,
    getRepairView
} from "@/apis/workOrder.js";

import {
    deepClone
} from "@/util";

export default {
    data() {
        return {
            editId: '',
            photosMaxQty: 5,
            showPhrase: false,
            showSalesmanCommission: false,
            showSalesman: false,
            showCustomer: false,
            showCustomerType: false,
            showRepairType: false,
            showRepairCatg: false,
            showAppointment: false,
            phraseList: [],
            appointment: '',
            custTypeList: ['公司', '家庭'],
            repairCatg: [],
            repairType: [],
            serviceList: [{}, {}, {}, {}, {}],
            currEmployee: '',
            employeeParams: {
                PageSize: 10,
                PageNo: 1,
                DeptId: '',
                GroupId: '',
                EmpName: '',
            },
            employeeList: [],
            partnerParams: {
                PageSize: 10,
                PartnerName: '',
                PageNo: 1,
                PartnerAttr: 'cust',
            },
            partnerName: '',
            partnerList: [],

            showAddress: false,
            addrText: '',
            showRegionPicker: false,
            columns: [],
            columnData: [],
            provinces: [],
            zoneInfo: [],

            formHeightList: [],
            tabsStyle: {
                activeStyle: {
                    color: '#F65D30',
                    fontWeight: 'bold',
                    transform: 'scale(1.05)'
                },
                inactiveStyle: {
                    color: '#333333',
                    transform: 'scale(1)'
                }
            },
            currTab: 0,
            tabList: [{
                name: '客户信息'
            }, {
                name: '工单信息'
            }, {
                name: '服务信息'
            },
            {
                name: '费用明细'
            },
            ],
            imageList: [],
            orderForm: {
                Descr: '', // 下单记录
                AppointmentDate: '',
                AppointmentTime: '',
                SalerCharge: '',
                RewardAmount: '',
                TGID: '',
                VirtCharge1: '',
                VirtCharge2: '',
                VirtCharge3: '',
                VirtCharge4: '',
                VirtCharge5: '',
                Employee1: '',
                Employee2: '',
                Employee3: '',
                Employee4: '',
                Employee5: '',
                CustId: '',
                CustName: '',
                Zone1: '',
                Zone2: '',
                Zone3: '',
                Tel: '', //电话
                Zone5: '',
                Contact: '', //联系人
                Zone1_2: '',
                Zone2_2: '',
                Zone3_2: '',
                Tel2: '',
                Zone5_2: '', //详细地址
                Contact2: '',
                CallPhotos: [],
                Remark1: '',
                Remark2: '',
                Remark3: '',
                Remark4: '',
                Remark5: '',
                ItemId: '',
                ItemName: '',
                ItemStyle: '',
                ItemBrand: '',
                ItemCatg: '',
                SerialNo: '',
                BuyDate: '',
                ExpaireDate: '',
                OrderSrc: '',
                OrderNo: '',        // 来源单号
                CustType: '',       // 客户类型
                RepairType: '',     //  服务类型
                Saler: '',          // 业务员
                Priority: false,
                RepairCatg: '',     // 服务分类
                lnkRegion: '',      //区域
            },
            orderRules: {
                Tel: [{
                    required: true,
                    message: '请输入手机号',
                    trigger: ['change', 'blur'],
                },
                {
                    validator: (rule, value, callback) => {
                        return uni.$u.test.mobile(value);
                    },
                    message: '手机号码不正确',
                    trigger: ['change', 'blur'],
                }
                ],
                Contact: {
                    required: true,
                    message: '请输入联系人姓名',
                    trigger: ['blur', 'change']
                },
            },
            fileList4: [],
        };
    },
    // onPageScroll(res) {
    // 	console.log('onPageScroll', res)

    // },
    onLoad(option) {
        console.log('option', option)
        if (option.id) {
            this.editId = option.id
            this.fetchRepairView()
        }
        this.fetchEmployeeList()
    },
    methods: {
        async fetchRepairView() {
            const custTypeMap = {
                0: "",
                1: '公司',
                2: '家庭'
            }
            const res = await getRepairView({
                RepairId: this.editId
            })
            if (res.MsgCode != 0) return
            console.log('fetchRepairView------->', res)
            const orderForm = res.data
            orderForm.CustType = custTypeMap[res.data.CustType]
            orderForm.Descr = res.data.CallDescr
            orderForm.Expense01 = parseFloat(orderForm.Expense01 ? orderForm.Expense01 : 0)
            orderForm.Expense02 = parseFloat(orderForm.Expense02 ? orderForm.Expense02 : 0)
            orderForm.Expense03 = parseFloat(orderForm.Expense03 ? orderForm.Expense03 : 0)
            orderForm.Expense04 = parseFloat(orderForm.Expense04 ? orderForm.Expense04 : 0)
            orderForm.Expense05 = parseFloat(orderForm.Expense05 ? orderForm.Expense05 : 0)
            orderForm.Revenue01 = parseFloat(orderForm.Revenue01 ? orderForm.Revenue01 : 0)
            orderForm.Revenue02 = parseFloat(orderForm.Revenue02 ? orderForm.Revenue02 : 0)
            orderForm.Revenue03 = parseFloat(orderForm.Revenue03 ? orderForm.Revenue03 : 0)
            orderForm.Revenue04 = parseFloat(orderForm.Revenue04 ? orderForm.Revenue04 : 0)
            orderForm.Revenue05 = parseFloat(orderForm.Revenue05 ? orderForm.Revenue05 : 0)
            orderForm.VirtCharge1 = parseFloat(orderForm.VirtCharge1 ? orderForm.VirtCharge1: 0)
            orderForm.VirtCharge2 = parseFloat(orderForm.VirtCharge2 ? orderForm.VirtCharge2: 0)
            orderForm.VirtCharge3 = parseFloat(orderForm.VirtCharge3 ? orderForm.VirtCharge3: 0)
            orderForm.VirtCharge4 = parseFloat(orderForm.VirtCharge4 ? orderForm.VirtCharge4: 0)
            orderForm.VirtCharge5 = parseFloat(orderForm.VirtCharge5 ? orderForm.VirtCharge5: 0)
            orderForm.SalerId = orderForm.Saler
            orderForm.Saler = orderForm.SalerName
            orderForm.Employee1 = orderForm.Engineer1Name
            orderForm.Employee2 = orderForm.Enginer2Name
            orderForm.Employee3 = orderForm.Enginer3Name
            orderForm.Employee4 = orderForm.Enginer4Name
            orderForm.Employee5 = orderForm.Enginer5Name

            delete orderForm.RepairDone
            delete orderForm.CreditDetail
            orderForm.CallPhotos = res.data.CallPhotos == '' ? [] : res.data.CallPhotos.split(',')
            this.imageList = orderForm.CallPhotos.map(item => {
                return { url: 'http://img.help10000.com' + item }
            })
            this.appointment = orderForm.AppointmentTime
            orderForm.lnkRegion = ''
            this.orderForm = orderForm

        },
        async handleOrderAdd() {
            const orderForm = deepClone(this.orderForm)

            const orderAuditForm = {}
            orderAuditForm.Saler = orderForm.Saler == orderForm.SalerName ? orderForm.SalerId : this.employeeList.find(o => o.EmpName === orderForm.Saler)?.EmpId || ''
            orderAuditForm.RepairIds = orderForm.ID
            orderAuditForm.Descr = orderForm.Repairdescr
            orderAuditForm.SalerCharge = orderForm.Expense02
            orderAuditForm.VirtCharge1 = orderForm.VirtCharge1
            orderAuditForm.VirtCharge2 = orderForm.VirtCharge2
            orderAuditForm.VirtCharge3 = orderForm.VirtCharge3
            orderAuditForm.VirtCharge4 = orderForm.VirtCharge4
            orderAuditForm.VirtCharge5 = orderForm.VirtCharge5
            orderAuditForm.Revenue01 = orderForm.Revenue01
            orderAuditForm.Revenue02 = orderForm.Revenue02
            orderAuditForm.Revenue03 = orderForm.Revenue03
            orderAuditForm.Revenue04 = orderForm.Revenue04
            orderAuditForm.Revenue05 = orderForm.Revenue05
            orderAuditForm.Revenue01Name = '服务费'
            orderAuditForm.Revenue02Name = '销售商品'
            orderAuditForm.Revenue03Name = '搬运费'
            orderAuditForm.Revenue04Name = '家具拆装费'
            orderAuditForm.Revenue05Name = '超重费'
            orderAuditForm.Expense03 = orderForm.Expense03
            orderAuditForm.Expense04 = orderForm.Expense04
            orderAuditForm.Expense05 = orderForm.Expense05
            orderAuditForm.Expense03Name = '商品成本'
            orderAuditForm.Expense04Name = '停车费'
            orderAuditForm.Expense05Name = '过路费'
            
            console.log('orderAuditForm>>>>>>>>>>>', orderAuditForm)
            const res = await orderAudit(orderAuditForm)
            console.log('handleAdd res', res)

            if (res.MsgCode != 0) return
            uni.showToast({
                title: '审核成功',
                icon: "none",
                mask: true,
                position: "center",
            });
            setTimeout(() => {
                this.$navigateBack(this.$router, 1)
            }, 1000)

        },
        handleServicePick(key) {
            console.log('handleServicePick', key)
            this.currEmployee = key
            this.showSalesman = true
        },
        handleEmployeeSearch() {
            this.employeeParams.PageNo = 1
            this.employeeList = []
            this.fetchEmployeeList()
        },
        scrolltoEmployeeLower() {
            this.employeeParams.PageNo = this.employeeParams.PageNo + 1
            this.fetchEmployeeList()

        },
        async fetchEmployeeList() {
            const res = await getEmployeeList(this.employeeParams)
            if (res.MsgCode != 0) return
            console.log('fetchEmployeeList', res)

            this.employeeList = [...this.employeeList, ...res.data.EmployeeList]
        },

        chargeSummation() {
            const form = this.orderForm
            console.log('chargeSummation',form)

            form.Expense01 = (form.VirtCharge1 + form.VirtCharge2 + form.VirtCharge3 + form.VirtCharge4 + form.VirtCharge5).toFixed(2)
            this.orderForm = form
        },
        revenueSummation() {
            const form = this.orderForm
            form.TotalRevenue = (form.Revenue01 + form.Revenue02 + form.Revenue03 + form.Revenue04 + form.Revenue05).toFixed(2)
            console.log('revenueSummation',form)
            this.orderForm = form
        },
        expensesSummation() {
            const form = this.orderForm
            form.TotalExpense = (form.Expense01 + form.Expense02 + form.Expense03 + form.Expense04 + form.Expense05).toFixed(2)
            console.log('expensesSummation',form)
            this.orderForm = form
        }
    }
}
